/*
 * @(#)ExternalIdentifierTypeTransformer.java
 * Date 2013-09-02
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.transformer.xds;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import oasis.names.tc.ebxml_regrep.xsd.rim._3.ExternalIdentifierType;

import us.hornerscorners.lamppost.hl7v3common.utils.HL7Utils;

import us.hornerscorners.lamppost.model.common.IClassifiableModel;
import us.hornerscorners.lamppost.model.common.InstanceIdentifier;

import us.hornerscorners.lamppost.registry.constant.Metadata;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-02
 * @author         Jim Horner
 */
@ApplicationScoped
public class ExternalIdentifierTypeTransformer {

    /** Field description */
    @Inject
    private InternationalStringTypeTransformer internationalStringTransformer;

    /** Field description */
    @Inject
    private VersionInfoTypeTransformer versionInfoTransformer;

    /**
     * Constructs ...
     *
     */
    public ExternalIdentifierTypeTransformer() {
        super();
    }

    /**
     * Method description
     *
     *
     * @param metadata
     * @param registryObject
     * @param model
     * @param value
     *
     * @return
     */
    private ExternalIdentifierType transform(Metadata metadata,
            IClassifiableModel registryObject, InstanceIdentifier model, String value) {

        ExternalIdentifierType result = new ExternalIdentifierType();

        result.setId(model.getEntryURN());

        // versioning
        result.setLid(model.getEntryLogicalURN());
        result.setVersionInfo(
            this.versionInfoTransformer.transform(model.getEntryVersion()));

        result.setIdentificationScheme(metadata.getURN());
        result.setRegistryObject(registryObject.getEntryURN());
        result.setValue(value);
        result.setName(
            this.internationalStringTransformer.transform(model.getDisplayNames()));
        result.setDescription(
            this.internationalStringTransformer.transform(model.getDescriptions()));

        result.setObjectType(Metadata.ExternalIdentifierObjectType.getURN());

        return result;
    }

    /**
     * Method description
     *
     *
     * @param metadata
     */
    public ExternalIdentifierType transformPatientIdentifier(Metadata metadata,
            IClassifiableModel registryObject, InstanceIdentifier pid) {

        String pidcx = HL7Utils.toCXString(pid);

        return transform(metadata, registryObject, pid, pidcx);
    }

    /**
     * Method description
     *
     *
     * @param metadata
     * @param registryObject
     * @param oid
     *
     * @return
     */
    public ExternalIdentifierType transformUniqueIdentifier(Metadata metadata,
            IClassifiableModel registryObject, InstanceIdentifier oid) {

        return transform(metadata, registryObject, oid, oid.getRoot());
    }
}
